﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
namespace GameVLTK
{
    public class TiledLayer : VisibleGameEntity
    {
        private int _nRows;

        public int nRows
        {
            get { return _nRows; }
            set { _nRows = value; }
        }
        private int _nCols;

        public int nCols
        {
            get { return _nCols; }
            set { _nCols = value; }
        }

        private int _CellWidth;

        public int CellWidth
        {
            get { return _CellWidth; }
            set { _CellWidth = value; }
        }
        private int _CellHeight;

        public int CellHeight
        {
            get { return _CellHeight; }
            set { _CellHeight = value; }
        }

        private int[,] _CellMatrix;

        public int[,] CellMatrix
        {
            get { return _CellMatrix; }
            set { _CellMatrix = value; }
        }

        //Mảng các cell
        private Cell[,] _Cells;

        protected Cell[,] Cells
        {
            get { return _Cells; }
            set { _Cells = value; }
        }

        public TiledLayer(string fileName)
        {
            StreamReader sr = new StreamReader(fileName);

            string[] arr = sr.ReadLine().Split(' ');

            this._nRows = Convert.ToInt32(arr[0]);
            this._nCols = Convert.ToInt32(arr[1]);

            this._CellMatrix = new int[this._nRows, this._nCols];

            int i = 0;
            while (!sr.EndOfStream)
            {
                arr = sr.ReadLine().Split(' ');

                for (int j = 0; j < this._nCols; j++)
                { 
                   this._CellMatrix[i, j] = Convert.ToInt32(arr[j]);  
                }

                i++;
            }
            sr.Close();
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_content"></param>
        /// <param name="strCellTextures"></param>
        /// <param name="topLeft"></param>
        /// <param name="cellWidth"></param>
        /// <param name="cellHeight"></param>
        /// <param name="_size"></param>
        public void Init(ContentManager _content, String[] strCellTextures, Vector2 topLeft,
            int cellWidth, int cellHeight)
        {
            this.TopLeft = topLeft;
          
            this.Sprites = new List<MySprite>();
            this.nSprites = 0;

            this.CellWidth = cellWidth;
            this.CellHeight = cellHeight;
            this.Cells = new Cell[_nRows, _nCols];

            
            for (int i = 0; i < this.nRows; i++)
                for (int j = 0; j < this.nCols; j++)
                    this.Cells[i, j] = new Cell(_content, strCellTextures[this.CellMatrix[i, j]],
                          new Vector2(j * cellWidth, i * cellHeight), new Vector2(cellWidth, cellHeight));

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="spriteBatch"></param>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < this._nRows; i++)
                for (int j = 0; j < this._nCols; j++)
                {
                    //Khi Map thay đổi thì phải báo lại cho thằng con cập 
                    //nhật lại vị trí của mình
                    Vector2 temp = new Vector2();
                    temp.X = j * this.CellWidth + Global.TopLeft.X;
                    temp.Y = i * this.CellHeight +Global.TopLeft.Y;
                    this._Cells[i, j].TopLeft = temp; 

                    this._Cells[i, j].Draw(gameTime, spriteBatch);
                   
                }
        }

    }
}
